<template>
  <div class="container">
  </div>
</template>

<script>
  import { bindMember } from '@/api/home'
  import { getAuth } from '@/api/login'
  import { setToken, setCustomerId, setPhone, setVeekrayStatus } from '@/utils/auth'

  export default {
    data() {
      return {
        isComponents: true,
        employee: {}
      }
    },
    onShow() {
      const employeeId = this.getCurrentPage().query.employeeId
      const token = this.getCurrentPage().query.token
      if (employeeId && token) {
        this.employee.employeeId = employeeId
        this.employee.token = token
        // 登录
        this.wxLogin()
      }
    },
    methods: {
      wxLogin() {
        const that = this
        that.$utils.showToast({
          title: '加载中',
          icon: 'loading',
          duration: 3000
        })
        that.$pt.login({
          success(res) {
            if (res.code) {
              console.log('employee-login:' + res.code)
              getAuth(res.code).then((response) => {
                if (response.isError) return
                if (!response.data.customerId) {
                  that.$utils.showToast({ title: '请先授权，再进行绑定！' })
                  setTimeout(() => {
                    that.$pt.reLaunch({ url: '/pages/home/main' })
                  }, 500)
                  return
                }
                setToken(response.data.token)
                setCustomerId(response.data.customerId)
                if (response.data.phone) {
                  setPhone(response.data.phone)
                }
                setVeekrayStatus(response.data.veekrayStatus)
                // 关闭loading
                that.$pt.hideToast()
                that.doBind()
              }).catch(() => {
              })
            } else {
              that.$utils.showToast({ title: '登录失败' })
            }
          }
        })
      },
      doBind() {
        if (this.employee.employeeId) {
          this.employee.memberId = this.getCustomerId
          bindMember(this.employee).then((response) => {
            if (response.isError) {
              this.$pt.reLaunch({ url: '/pages/home/main' })
            } else {
              this.$utils.showToast({ title: '绑定成功' })
              setVeekrayStatus(1)
              this.globalData.firstBuyCard = true
              setTimeout(() => {
                this.$utils.setMyId(this.getCustomerId)
                this.$root.$mp.appOptions.query = {}
                this.$pt.reLaunch({ url: '/pages/home/main' })
                // this.$pt.reLaunch({ url: '/pages/home/main?memberId=' + this.getCustomerId })
              }, 500)
            }
          }).catch(() => {
          })
        }
      }
    }
  }
</script>

<style scoped>

</style>
